package com.shangyueshang.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/24 19:05
 * @version: v1.0.0
 * <p>
 * 出参保留两位小数
 * </p>
 */
public class DeserializerBigDecimal extends JsonDeserializer<BigDecimal> {

    @Override
    public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        if (Objects.isNull(jsonParser.getDecimalValue())) {
            return BigDecimal.ZERO.setScale(2);
        } else {
            // 这里取floor
            // return jsonParser.getDecimalValue().setScale(2, RoundingMode.FLOOR);
            return jsonParser.getDecimalValue().setScale(2);
        }
    }
}
